package com.cbg.sys.controller;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cbg.base.constant.Constant;
import com.cbg.base.exception.MiException;
import com.cbg.base.utils.CodeMsg;
import com.cbg.base.utils.Result;
import com.cbg.bus.utils.OrderUtils;
import com.cbg.sys.entity.LineCar;
import com.cbg.sys.entity.SeatPrice;
import com.cbg.sys.entity.SeatStatus;
import com.cbg.sys.service.ILineCarService;
import com.cbg.sys.service.ISeatPriceService;
import com.cbg.sys.utils.PriceUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;
import com.cbg.base.core.SuperController;
import java.util.*;
import java.util.stream.IntStream;

/**
 * <p>
 * 座位价格表 前端控制器
 * </p>
 *
 * @author chenbing
 * @since 2018-12-18
 */
@RestController
@RequestMapping("/sys/seat")
@Api(value = "车座控制器",tags = "车座操作接口")
public class SeatPriceController extends SuperController<ISeatPriceService, SeatPrice> {
    @Autowired
    ILineCarService lineCarService;
    @Autowired
    ISeatPriceService seatPriceService;

    @ApiOperation(value="选中车型后，展示座位，价格", notes="选中车型后，展示座位，价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "班次ID", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "typeId", value = "车型ID", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "seatNum", value = "座位总数", required = true, dataType = "Integer",paramType = "query")
    })
    @GetMapping("/show")
    public Result<?> showSeat(String lineId, String typeId, Integer seatNum){
        Objects.requireNonNull(typeId,CodeMsg.IllegalTypeArg.getMsg());
        SeatPrice seatPrice = PriceUtils.esurePriceByLineId(lineId, typeId);
        LineCar car = lineCarService.getOne(new LambdaQueryWrapper<LineCar>()
                .eq(LineCar::getLineId, lineId).eq(LineCar::getTypeId, typeId));
        List<SeatStatus> seatStatuses=new ArrayList<>(seatNum);
        IntStream.range(0,seatNum).forEach(x->{
            String binaryStatus;
            StringBuffer sb=new StringBuffer(binaryStatus=(NumberUtil.getBinaryStr(car.getStatus())));
            if(binaryStatus.length()<seatNum)
                IntStream.range(0,seatNum-binaryStatus.length()).forEach(e->sb.insert(0,Constant.NO_BOOK));
            seatStatuses.add(new SeatStatus(x,sb.reverse().toString().charAt(x)== Constant.SEAT_BOOK,
                    PriceUtils.esurePriceBySeatId(x, seatPrice)));
        });
        return Result.ok(seatStatuses);
    }

    @ApiOperation(value="订座", notes="价格必须在后端计算")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "lineId", value = "班次ID", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "typeId", value = "车型ID", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "riderIds", value = "乘车人ID列表", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "seatIds", value = "座位ID列表", required = true, dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "price", value = "总价", required = true, dataType = "BigDecimal",paramType = "query"),
            @ApiImplicitParam(name = "rideAddress", value = "上车地址", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "contactName", value = "紧急联系人名字", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "contactTel", value = "紧急联系人电话", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "debusAddress", value = "下车地址", required = true, dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "discountId", value = "优惠券id", dataType = "String",paramType = "query")
    })
    @PostMapping("/book")
    public Result<?> bookSeat(String lineId, String typeId, List<String> riderIds, List<Integer> seatIds,
        String contactName,String contactTel,String rideAddress, String debusAddress,@Nullable String discountId){
        String outTradeNo = OrderUtils.out_trade_no();
        if(!seatPriceService.bookSeat(userId(),lineId,typeId,riderIds,seatIds,contactName,contactTel,rideAddress,debusAddress,discountId,outTradeNo))
            throw new MiException(CodeMsg.OrderFail.getMsg());
        return Result.ok(outTradeNo);
    }
}
